package at.main.gui;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import at.main.android.Graphics;
import at.main.android.Sprite;
import at.main.util.AllImage;
import at.main.util.AllSound;
import at.main.util.ConfigPanel;
import at.main.util.ManagerFont;

public class PlayGame {
	public static boolean isReady = false;

	public static boolean isPacMan = true;

	public static boolean isGameOver = false;

	public static boolean isGameWin = false;

	public static boolean isTryAgain = false;

	public static boolean isContineLevle = false;

	public static boolean isPause = false;

	public int iColCamera = 0;

	public int iRowRamera = 0;

	public int iStartColCamera = 0;

	public int iStartRowCamera = 0;

	public int iWithMap = 0;

	public int iHeightMap = 0;

	public int iXVat = 0;

	public int iYVat = 0;

	public static long iTotalSorce = 0;

	// public SuperMan superman;
	public PacMan pacMan;

	public ManagerMonster managerMonster;

	public GameOverPage gameOver;

	public GameWinPage gameWin;

	public static final int k_column = 30;

	public static final int k_row = 30;

	public static int[][] arrMap = new int[k_row][k_column];

	public static int iCountItems = 0;

	

	public PlayGame() {
		if(SoundPage.isSound)
			AllSound.aBeginLevel.playSound();
		iColCamera = MyCanvas.iWidth / 24;
		iRowRamera = MyCanvas.iHeight / 24;
		iTotalSorce = 0;
		isTryAgain = false;
		isGameWin = false;
		isContineLevle = false;
		isReady = false;
		pacMan = null;
		pacMan = new PacMan();

//		pacMan.iLevel = ContinePage.iLevel;
//		pacMan.iScore = ContinePage.iTotalScore;
//		pacMan.iV = ContinePage.iV;
		iCountItems = 0;
		try {
			loadMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
		managerMonster = null;
		managerMonster = new ManagerMonster();
		gameOver = new GameOverPage();
		// gameWin = new GameWinPage();
		isPause = false;
	}
	
	// Qua level
	public void PlayGameNewLevel() {
		if(SoundPage.isSound)
			AllSound.aBeginLevel.playSound();
		isTryAgain = false;
		iColCamera = MyCanvas.iWidth / 24;
		iRowRamera = MyCanvas.iHeight / 24;
		int v = pacMan.iV + 2; // van toc cua con pacman
		iTotalSorce += pacMan.iScore;
		pacMan.iLevel++;
		int level = pacMan.iLevel;
		if (level == 4) {
			gameWin = new GameWinPage();
			isGameWin = true;
			return;
		}
		iCountItems = 0;
		pacMan = new PacMan(v, iTotalSorce, level);
		try {
			loadMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
		v = managerMonster.iV + 2; // Voc toc monster
		int c = managerMonster.iCountMonster;
		managerMonster = new ManagerMonster(v, c);
		gameOver = new GameOverPage();
		isPause = false;
	}

	// Choi lai
	public void PlayGameTryAgianLevel() {
		isTryAgain = false;
		iColCamera = MyCanvas.iWidth / 24;
		iRowRamera = MyCanvas.iHeight / 24;
		int v = pacMan.iV; // van toc cua con pacman

		long sroce = pacMan.iScore - iTotalSorce;
		int level = pacMan.iLevel;

		iCountItems = 0;
		pacMan = new PacMan(v, iTotalSorce, level);
		try {
			loadMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
		v = managerMonster.iV; // Voc toc monster
		int c = managerMonster.iCountMonster;
		managerMonster = new ManagerMonster(v, c);
		gameOver = new GameOverPage();
		// gameWin = new GameWinPage();
	}
	public void PlayGameContine(){
		
	}
	public void loadMap() throws IOException {
		InputStream is = "".getClass().getResourceAsStream(
				"/Level1.dat");

		try {
			ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
			int ch;
			while ((ch = is.read()) != -1) {
				bStrm.write(ch);
			}
			byte imageData[] = bStrm.toByteArray();

			for (int i = 0; i < imageData.length; i++) {
				if (imageData[i] == 1)
					iCountItems++;
				int column = i % k_column;
				int row = (i - column) / k_row;
				arrMap[row][column] = imageData[i];
			}
			bStrm.close();
		} finally {
			if (is != null)
				is.close();
		}
	}

	public void paintMap(Graphics g) {
		int i, j;
		for (i = pacMan.iStartRow; i < pacMan.iEndRow; i++) {
			for (j = pacMan.iStartCol; j < pacMan.iEndCol; j++) {
				g.drawRegion(AllImage.img_Title, 0,
						0 + (arrMap[i][j] - 1) * 24, 24, 24, Sprite.TRANS_NONE,
						24 * j, ConfigPanel.heightPanel() + i * 24,
						Graphics.TOP | Graphics.LEFT);
			}
		}

	}

	boolean isFlagReady = true;

	// Thoi gian dislay cho chu san sang
	int iDislayReady = 0;

	public void paintReady(Graphics g) {
		iDislayReady++;
		if (iDislayReady % 10 <= 3) {
			g.drawImage(AllImage.img_ready, MyCanvas.iWidth / 2
					+ MyCanvas.iTranslateX - AllImage.img_ready.getWidth() / 2,
					MyCanvas.iHeight / 2 + MyCanvas.iTranslateY
							- AllImage.img_ready.getHeight() / 2, Graphics.TOP
							| Graphics.LEFT);
		}
		if (iDislayReady % 10 == 0)
			iDislayReady = 0;

	}

	public void paintContineLevel(Graphics g) {
		g.drawImage(AllImage.img_msg, MyCanvas.iWidth / 2
				+ MyCanvas.iTranslateX - AllImage.img_msg.getWidth() / 2,
				MyCanvas.iHeight / 2 + MyCanvas.iTranslateY
						- AllImage.img_msg.getHeight() / 2, Graphics.TOP
						| Graphics.LEFT);
		int x = MyCanvas.iWidth / 2 + MyCanvas.iTranslateX
				- AllImage.img_msg.getWidth() / 2 + 20;
		int y = MyCanvas.iHeight / 2 + MyCanvas.iTranslateY
				- AllImage.img_msg.getHeight() / 2 + 20;
		String s = "Level: " + String.valueOf(pacMan.iLevel);
		ManagerFont.drawString(g, s, x, y, 0, 1);
		s = "Điểm: " + String.valueOf(pacMan.iScore - iTotalSorce);
		ManagerFont.drawString(g, s, x, y + 15, 0, 1);
		s = "Số mấn: " + String.valueOf(pacMan.iCountPickup);
		ManagerFont.drawString(g, s, x, y + 30, 0, 1);
		s = "Số Monster: " + String.valueOf(pacMan.iCountMonsterEta);
		ManagerFont.drawString(g, s, x, y + 45, 0, 1);
		s = "Số Items: " + String.valueOf(pacMan.iCountItemsEta);
		ManagerFont.drawString(g, s, x, y + 60, 0, 1);
	}

	public void paintPanel(Graphics g) {
		// Set vung ve Panel
		g.setClip(0, 0, MyCanvas.iWidth, ConfigPanel.heightPanel());
		g.setColor(255, 255, 255);
		g.drawImage(AllImage.img_cmd, 0, 0, Graphics.TOP | Graphics.LEFT);
		// Draw sroce
		ManagerFont.drawString(g, String.valueOf(PacMan.iScore), ConfigPanel
				.widthSroce(), ConfigPanel.heighPanel1(), 0, 1);
		// Dram level
		ManagerFont.drawString(g, String.valueOf(PacMan.iLevel), ConfigPanel
				.widthLevel(), ConfigPanel.heighPanel1(), 0, 1);

		// Draw icon
		for (int i = 0; i < PacMan.iCountlife; i++) {
			g.drawImage(AllImage.img_icon, ConfigPanel.widtdIcon()
					+ (i * AllImage.img_icon.getWidth()), 2, Graphics.TOP
					| Graphics.LEFT);
		}
	}

	// Dich chuyen map
	public void MoveMap(Graphics g) {
		g.translate(-MyCanvas.iTranslateX, -MyCanvas.iTranslateY);
	}

	public void paint(Graphics g) {
		// Kiem tra da san sang cho game chu
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, MyCanvas.iWidth, MyCanvas.iHeight);
		if (isGameWin) {
			gameWin.paint(g);
		} else {
			// Goi ve panel
			paintPanel(g);

			// Set vung ve game
			g.setClip(0, ConfigPanel.heightPanel(), MyCanvas.iWidth,
					MyCanvas.iHeight);

			// Dich chuyen map
			MoveMap(g);
			g.fillRect(MyCanvas.iTranslateX, MyCanvas.iTranslateY,
					MyCanvas.iWidth, MyCanvas.iHeight);

			// g.setColor(0, 0, 0);
			if (arrMap != null) {
				paintMap(g);
			}
			pacMan.paint(g);
			if (!pacMan.isDie) {
				managerMonster.paint(g);
			}

			// Check cac kiem tra
			if (!isReady && !isGameOver && !isGameWin) {
				paintReady(g);
			} else {
				AllSound.aBeginLevel.stopSound();
				if(SoundPage.isSound && OptionPage.isSoundBackground)
					AllSound.aBackground.playSoundBG();
			}
			if (isContineLevle /* && !isGameWin */) {
				paintContineLevel(g);
			}
			if (isGameOver) {
				gameOver.paint(g);
			}			
			// Paint icon menu
			if (AllImage.img_menu != null)
				g.drawImage(AllImage.img_menu, 0 + MyCanvas.iTranslateX,
						MyCanvas.iHeight + MyCanvas.iTranslateY
								- AllImage.img_menu.getHeight(), Graphics.TOP
								| Graphics.LEFT);
		}
		
	}

	// Update chinh cua playgame
	public void update() {
		if (MyCanvas.keyHold[5] && !isReady) {
			isReady = true;
		}
		if (MyCanvas.keyHold[12]
				&& MyCanvas.iCurrentPage == MyCanvas.k_PlayGame) {
			isPause = true;
			MyCanvas.iCurrentPage = MyCanvas.k_MenuPlaying;
			MyCanvas.clearInput();
		}
		if (isGameWin) {
			gameWin.update();
		}
		if (!isGameOver) {
			if (!isContineLevle) {
				if (isReady) {
					if (!isPause) {
						pacMan.update();
						if (!pacMan.isDie) {
							managerMonster.update();
						}
					}
				}
			} else {
				if (MyCanvas.keyHold[5]) {
					isContineLevle = false;
					isReady = false;
					PlayGameNewLevel();
					MyCanvas.clearInput();
				}
			}
			// Kiem tra cho coi lai
			if (isTryAgain) {
				isContineLevle = false;
				isReady = false;
				PlayGameTryAgianLevel();
				MyCanvas.clearInput();
			}
		} else { // That bai
			gameOver.update();
		}
	}
}
